package com.ruoyi.framework.sharding;

import org.apache.shardingsphere.driver.jdbc.core.datasource.ShardingSphereDataSource;

import java.util.*;

/**
 * @Description: 分片表缓存枚举
 */
public enum ShardingTableCache {

    electricity_history("electricity_history", new HashSet<>()),
    safety_point_real_history("safety_point_real_history", new HashSet<>()),
    ryss_history("ryss_history", new HashSet<>()),
    belt_history("belt_history", new HashSet<>()),
    aircompressor_history("aircompressor_history", new HashSet<>()),
    auxiliary_fan_history("auxiliary_fan_history", new HashSet<>()),
    centralpumphouse_history("centralpumphouse_history", new HashSet<>()),
    gas_drainage_pump_room_history("gas_drainage_pump_room_history", new HashSet<>()),
    excavation_data_history("excavation_data_history", new HashSet<>()),
    jingxiafenzhan_history("jingxiafenzhan_history", new HashSet<>()),
    jingxia_history("jingxia_history", new HashSet<>()),
    mainventilator1_history("mainventilator1_history", new HashSet<>()),
    producing_nitrogen_history("producing_nitrogen_history", new HashSet<>()),
    ky_lane_mgyl_history("ky_lane_mgyl_history", new HashSet<>()),
    ky_lane_dblc_history("ky_lane_dblc_history", new HashSet<>()),
    monkey_vehicle_history("monkey_vehicle_history", new HashSet<>()),
    use_electricity_history("use_electricity_history", new HashSet<>());

    /**
     * 逻辑表名
     */
    private final String logicTableName;
    /**
     * 实际表名
     */
    private final Set<String> resultTableNamesCache;

    private static Map<String, ShardingTableCache> valueMap = new HashMap<>();

    static {
        Arrays.stream(ShardingTableCache.values()).forEach(o -> valueMap.put(o.logicTableName, o));
    }

    ShardingTableCache(String logicTableName, Set<String> resultTableNamesCache) {
        this.logicTableName = logicTableName;
        this.resultTableNamesCache = resultTableNamesCache;
    }

    public static ShardingTableCache of(String value) {
        return valueMap.get(value);
    }

    public String logicTableName() {
        return logicTableName;
    }

    public Set<String> resultTableNamesCache() {
        return resultTableNamesCache;
    }

    /**
     * 更新缓存、配置（原子操作）
     *
     * @param tableNameList
     * @param shardingSphereDataSource
     */
    public void atomicUpdateCacheAndActualDataNodes(List<String> tableNameList, ShardingSphereDataSource shardingSphereDataSource) {
        synchronized (resultTableNamesCache) {
            // 删除缓存
            resultTableNamesCache.clear();
            // 写入新的缓存
            resultTableNamesCache.addAll(tableNameList);
            // 动态更新配置 actualDataNodes
            ShardingAlgorithmTool.actualDataNodesRefresh(logicTableName, tableNameList, shardingSphereDataSource);
        }
    }

    public static Set<String> logicTableNames() {
        return valueMap.keySet();
    }

    @Override
    public String toString() {
        return "ShardingTableCache{" +
                "logicTableName='" + logicTableName + '\'' +
                ", resultTableNamesCache=" + resultTableNamesCache +
                '}';
    }
}
